package cn.texous.demo.dmqs.producer;

import cn.texous.demo.dmqs.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * kafka producer
 *
 * @author leven
 * @since
 */
@Slf4j
@Component
public class KafkaProducer {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 发送消息
     * @param topicName topic
     * @param key key
     * @param msgParam params
     */
    public void send(String topicName, String key, Object msgParam) {
        String msg = JsonUtils.object2String(msgParam);
        try {
            kafkaTemplate.send(topicName, key, msg);
            log.info("succ to send msg:{}", msg);
        } catch (Exception e) {
            log.error("fail to send msg:{}", msg);
        }
    }

}
